package com.lyl.yeb.controller;

import com.lyl.yeb.utils.RespBean;
import com.lyl.yeb.entity.Admin;
import com.lyl.yeb.service.AdminService;
import com.lyl.yeb.vo.LoginParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;

/**
 * @author lylbell
 * @date 2022-03-02 15:59
 */
@RestController
public class LoginController {


    @Autowired
    private AdminService adminService;

    /**
     * 登录
     * @param loginParam
     * @return
     */
    @PostMapping("/login")
    public RespBean login(@RequestBody LoginParam loginParam) {
        return adminService.login(loginParam.getUsername(),loginParam.getPassword());
    }


    /**
     * 获取当前用户信息
     * @param principal
     * @return
     */
    @GetMapping("/admin/info")
    public Admin getAdminInfo(Principal principal) {
        if (principal == null) {
            return null;
        }
        String username = principal.getName();
        Admin admin = adminService.getAdminByUsername(username);
        admin.setPassword(null);
        return admin;
    }

    /**
     * 退出登录
     * @return
     */
    @PostMapping("/logout")
    public RespBean logout() {
        return RespBean.success("退出成功");
    }
}
